
package fy.input.conf;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import fy.util.MyUtil;

/**
 * OA扩张控件的配置文件操作
 * 
 * @author zc
 * 
 */
public final class InputConfManager {

	private static InputConfManager ic;

	// private static final Map<String, String> inputsMap = new HashMap<String,
	// String>();

	private static Log log = LogFactory.getLog(InputConfManager.class);

	private Map<String, String> urlMap = new HashMap<String, String>();

	private InputConfManager() {
		init();
	}

	public static InputConfManager getInstance() {
		if (ic == null) {
			synchronized (InputConfManager.class) {
				if (ic == null) {
					ic = new InputConfManager();
				}
			}
		}

		return ic;
	}

	private void init() {
		String rootPath = MyUtil.getRealPath(InputConfManager.class);
		String confPath = rootPath + "myInputExtend.xml";
		SAXReader sr = new SAXReader();
		try {
			Document doc = sr.read(new File(confPath));

			for (Iterator<?> iter = doc.getRootElement().elements().iterator(); iter
					.hasNext();) {
				Element node = (Element) iter.next();
				String id = node.attributeValue("id");
				String file = node.attributeValue("path");
				urlMap.put(id, file);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("读取通用扩张控件配置文件异常", e);
		}
	}

	public void reload() {
		urlMap.clear();
		init();		
	}

	public String getURL(String id) {
		return urlMap.get(id);
	}
	
	public static void main(String[] args) {
		System.out.println(InputConfManager.getInstance().getURL("lingliaofaliaodan"));
	}
}
